<?php

namespace Biz\model;

use Biz\tools\ShopArea;
use Doraemon\pockets\datebase\BaseDbModel;

class ShippingTempl extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop';

    const _DATABASE_ = 'shop';

    const _TABLE_NAME = 'shop_shipping_templ';

    const STATUS_PASSED = 1;//正常
    const STATUS_DELETE = 9;//删除

    const _SUB_TABLE = false;


    //计费类型
    const BILLING_CATE_PIECES = 1;//按件
    const BILLING_CATE_WEIGHT = 2;//按重量
    const BILLING_CATE_VOLUME = 3;//按体积

    /**
     * 计量单位
     * @var array
     */
    public static $billingUnitArr = [
        self::BILLING_CATE_PIECES => '件',
        self::BILLING_CATE_VOLUME => 'm³',
        self::BILLING_CATE_WEIGHT => 'kg'
    ];

    public static function addTempl($data)
    {
        self::insert($data);
        return self::id();
    }

    public static function updateTempl($data, $where)
    {
        return self::update($data, $where)->rowcount();
    }

    public static function getTempl($shopId, $status = null)
    {
        $where = ['shop_id' => $shopId];
        if (!is_null($status)) {
            $where['status'] = $status;
        }
        return self::select('*', $where);
    }

    public static function getTemplById($shopId, $templId, $status = null)
    {
        $where = [
            'shop_id' => $shopId,
            'id' => $templId
        ];
        if (!is_null($status)) {
            $where['status'] = $status;
        }

        $templ = self::select('*', $where);

        if (empty($templ)) {
            return [];
        }

        return array_shift($templ);
    }

    public static function getTemplByIds($templIds, $status = null)
    {
        $where = [
            'id' => $templIds
        ];
        if (!is_null($status)) {
            $where['status'] = $status;
        }
        $templ = self::select('*', $where);

        if (empty($templ)) {
            return [];
        }

        return $templ;
    }

    public static function getTemplInfoById($templId, $status = null)
    {
        $where = [
            'id' => $templId
        ];
        if (!is_null($status)) {
            $where['status'] = $status;
        }

        $templ = self::get('*', $where);

        return $templ;
    }

    public static function getTemplCount($shopId)
    {
        return self::count([
            'shop_id' => $shopId,
            'status' => self::STATUS_PASSED
        ]);
    }

    public static function getTemplinfos($ids)
    {
        $infos = self::select("*", [
            'id' => $ids,
            'status' => self::STATUS_PASSED
        ]);

        $temlpList = ShippingTemplAttr::getAttrsByTid($ids);

        $infos = array_map(function ($info) use ($temlpList) {
            $info['list'] = isset($temlpList[$info['id']]) ? $temlpList[$info['id']] : [];

            $info['shipping_address'] = ShopArea::locationId2Name(explode(",", $info['shipping_place'])[1]);
            if ($info['shipping_address'] == '市辖区' || count($info['shipping_place']) == 2) {
                $info['shipping_address'] = ShopArea::locationId2Name(explode(",", $info['shipping_place'])[0]);
            }
            return $info;
        }, $infos);
        return array_column($infos, null, 'id');

    }

    public static function getSupportAreas($ids)
    {
        $result = [];
        $arr = self::select(['id', 'support_area'], [
            'id' => $ids,
            'status' => self::STATUS_PASSED
        ]);
        $list = [];
        foreach ($arr as $row) {
            $list[$row['id']] = $row['support_area'];
        }
        foreach ($ids as $id) {
            if (isset($list[$id]) && $list[$id]) {
                $supportArea = explode(',', $list[$id]);
                $result[$id] = $supportArea;
            } else {
                $result[$id] = [];
            }
        }
        return $result;
    }
}